/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ipfw.cs590.cheat.struts.actions;

import edu.ipfw.cs590.cheat.AssignmentManagementInterface;
import edu.ipfw.cs590.cheat.AssignmentManagementService;
import edu.ipfw.cs590.cheat.GetAssignment;
import edu.ipfw.cs590.cheat.util.Authentication;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.xml.ws.soap.SOAPFaultException;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author SafarTimuro
 */
public class DownloadAction extends org.apache.struts.actions.DownloadAction {

    /**
     * Download action called.
     * @param mapping mapping.
     * @param form the form.
     * @param request the request.
     * @param response the response.
     * @return StreamInfo file.
     * @throws java.lang.Exception
     */
    @Override
    protected StreamInfo getStreamInfo(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        System.out.println("Downloading File...");

        try {
            String id = request.getParameter("id");
            System.out.println("id = " + id);

            // Call Web Service Operation
            AssignmentManagementService service = new AssignmentManagementService();
            AssignmentManagementInterface port = service.getAssignmentManagementSOAP();
            // Initialize arguments
            GetAssignment parameters = new GetAssignment();
            parameters.setUUID(id);

            String token = (String) request.getSession(false).getAttribute(Authentication.SESSION_NAME_TOKEN);
            System.out.println("token = " + token);

            // Download a "zip" file
            String contentType = "application/zip";
            System.out.println("Making call...");
            byte[] file = port.getAssignment(parameters, token).getResult().getValue();

            System.out.println("Call Processed...");
            // Set the content disposition
            response.setHeader("Content-disposition",
                    "attachment; filename=" + "Assignment" + id + ".zip");

            return new ByteArrayStreamInfo(contentType, file);
        } catch (SOAPFaultException ex) {
            response.sendError(response.SC_FORBIDDEN, ex.getMessage());
        } catch (Exception ex) {
            response.sendError(response.SC_FORBIDDEN, "File Not Found.");
        }


        String contentType = "text/plain";
        response.setHeader("Content-disposition",
                "attachment; filename=null.txt");
        //Nothing we have no clue
        return new ByteArrayStreamInfo(contentType, "".getBytes());
    }

    /**
     * Stream info class for returning byte arrays.
     */
    protected class ByteArrayStreamInfo implements StreamInfo {

        protected String contentType;
        protected byte[] bytes;

        /**
         * Constructor.
         * @param contentType content type.
         * @param bytes data.
         */
        public ByteArrayStreamInfo(String contentType, byte[] bytes) {
            this.contentType = contentType;
            this.bytes = bytes;
        }

        /**
         * Get the content type.
         * @return String content type.
         */
        public String getContentType() {
            return contentType;
        }

        /**
         * Return the input stream
         * @return InputStream wrapping bytes.
         * @throws java.io.IOException bad stuff.
         */
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(bytes);
        }
    }
}
